<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  //页面当中方法
  async function handleLogin() {
    try {
      let token = await login({
        mobile: '13800000002',
        password: '1234567'
      })
      console.log('token', token)
      console.log('跳转到首页')
    } catch (err) {
      console.log('err', err)
    }
  }
  //api里面方法
  function login(data) {
    return request({
      url: '/sys/login',
      method: 'post',
      data
    })
  }
  //request方法
  function request() {
    return new Promise((resolve, reject) => {
      //通过ajax获取服务器返回数据
      //成功的返回
      // let response = {data: {data: '123', success: true}}
      //失败的返回
      let response = {
        data: {
          success: false,
          message: '密码错误'
        }
      }
      resolve(interceptors_response(response))
    })
  }
  //响应拦截器
  function interceptors_response(response) {
    let {
      data: {
        data,
        success,
        message
      }
    } = response
    if (!success) {
      // console.log('提示信息', message)
      return Promise.reject(message)
    }
    return data
  }


  handleLogin()

</script>

</html>
